package com.a.controller;

import com.a.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.StringValueExp;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/9/28 19:20
 */
@RestController
public class TestController {
    private RedisTemplate redisTemplate;
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
//        redisTemplate.setValueSerializer(RedisSerializer.json());
//        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        redisTemplate.setHashValueSerializer(RedisSerializer.string());
    }

    @PostMapping("set")
    public String set(String key, String value){
        redisTemplate.opsForValue().set(key,value);
        return "success";
    }
    @PostMapping("user")
    public String set(User user){
        redisTemplate.opsForValue().set(user.getId(),user);
        return "success";
    }
}
